<template>
  <div style="padding: 10px; background-color: #f9f9f9; min-height: 100vh;">
    <!-- 功能区域 -->
    <div style="margin: 10px 0; display: flex; align-items: center;">
      <el-button type="primary" style="background-color: #ffaa50; border-color: #ffaa50;" @click="add">新增</el-button>
      <el-button type="danger" style="margin-left: 5px; background-color: #ffaa50; border-color: #ffaa50;" @click="batchDelete">批量删除</el-button>
    </div>

    <!-- 搜索区域 -->
    <div style="margin: 10px 0; display: flex; align-items: center;">
      <el-input v-model="search" placeholder="请输入关键词进行查询" style="width: 20%; margin-right: 10px;" clearable></el-input>
      <el-button type="primary" style="background-color: #ffaa50; border-color: #ffaa50;" @click="searchByRoomName">查询</el-button>
    </div>

    <!-- 表格区域 -->
    <el-table :data="tableData" border style="width: 100%; margin-top: 10px;" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="ID"/>
      <el-table-column prop="userId" label="身份证"/>
      <el-table-column prop="roomType" label="房间类型"/>
      <el-table-column prop="status" label="状态"/>
      <el-table-column prop="name" label="顾客名"/>
      <el-table-column prop="phone" label="电话号码"/>
      <el-table-column prop="checkinDate" label="入住日期" :formatter="formatDate"/>
      <el-table-column prop="orderTime" label="订购日期" :formatter="formatDate"/>
      <el-table-column prop="remarks" label="备注"/>
      <el-table-column label="操作">
        <template #default="scope">
          <div style="display: flex; justify-content: center;">
            <el-button type="text" style="color: #ffaa50;" @click="handleEdit(scope.row)">编辑</el-button>
            <el-popconfirm title="确认删除吗?" @confirm="handleDelete(scope.row.id)">
              <template #reference>
                <el-button type="text" style="color: #ffaa50;">删除</el-button>
              </template>
            </el-popconfirm>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <!-- 底部分页 -->
    <div style="margin: 10px 0; display: flex; justify-content: flex-end;">
      <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[5, 10, 20]"
          :small="small"
          :disabled="disabled"
          :background="background"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </div>

    <!-- 点击新增，弹出的对话框 -->
    <el-dialog
        v-model="dialogVisible"
        title="酒店预订信息"
        width="30%"
        :close-on-click-modal="false"
        :destroy-on-close="true"
    >
      <el-form :model="form" label-width="120px" style="padding: 20px;">
        <el-form-item label="身份证">
          <el-input v-model="form.userId" style="width: 80%;"></el-input>
        </el-form-item>
        <el-form-item label="房间类型">
          <el-input v-model="form.roomType" style="width: 80%;"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-input v-model="form.status" style="width: 80%;"></el-input>
        </el-form-item>
        <el-form-item label="顾客名">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="form.phone" style="width: 80%;"></el-input>
        </el-form-item>
        <el-form-item label="入住日期">
          <el-date-picker v-model="form.checkinDate" type="date" style="width: 80%;" format="yyyy-MM-dd" value-format="yyyy-MM-dd"></el-date-picker>
        </el-form-item>
        <el-form-item label="订购日期">
          <el-date-picker v-model="form.orderTime" type="date" style="width: 80%;" format="yyyy-MM-dd" value-format="yyyy-MM-dd"></el-date-picker>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remarks" style="width: 80%;"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" style="background-color: #ffaa50; border-color: #ffaa50;" @click="save">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import request from "@/utils/request";

export default {
  name: "HotelBooking",
  components: {},
  data() {
    return {
      form: {},
      dialogVisible: false,
      search: '',
      currentPage: 1,
      total: 10,
      pageSize: 10,
      tableData: [],
      selectedIds: [] // 存储选中的行 ID
    }
  },
  methods: {
    add() {
      this.form = {};
      this.dialogVisible = true;
    },
    handleEdit(row) {
      this.form = JSON.parse(JSON.stringify(row));
      this.dialogVisible = true;
    },
    handleDelete(id) {
      request.delete("/hotelBooking/deleteHotelBookings/" + id).then(res => {
        if (res.code === '0') {
          this.$message({
            type: "success",
            message: '删除成功'
          });
        } else {
          this.$message({
            type: "error",
            message: "删除失败"
          });
        }
        this.getBookingList();
      });
    },
    handleSizeChange() {
      this.getBookingList();
    },
    handleCurrentChange() {
      this.getBookingList();
    },
    getBookingList() {
      request.get("/hotelBooking/getHotelBookingsPage", {
        params: {
          pageNumber: this.currentPage,
          pageSize: this.pageSize,
          search: this.search
        }
      }).then(res => {
        this.total = res.data.total;
        this.tableData = res.data.list;
      });
    },
    batchDelete() {
      if (this.selectedIds.length === 0) {
        this.$message({
          type: 'warning',
          message: '请先选择要删除的行'
        });
        return;
      }
      request.delete("/hotelBooking/deleteHotelBookingsByIds", {
        data: this.selectedIds
      }).then(res => {
        if (res.code === '0') {
          this.$message({
            type: "success",
            message: '批量删除成功'
          });
          this.getBookingList();
          this.selectedIds = [];
        } else {
          this.$message({
            type: "error",
            message: "批量删除失败"
          });
        }
      });
    },
    handleSelectionChange(selection) {
      this.selectedIds = selection.map(item => item.id);
    },
    save() {
      if (this.form.id) {
        request.put("/hotelBooking/updateHotelBookings", this.form).then(res => {
          if (res.code === '0') {
            this.$message({
              type: "success",
              message: '编辑成功'
            });
            this.dialogVisible = false;
            this.getBookingList();
          } else {
            this.$message({
              type: "error",
              message: "编辑失败"
            });
          }
        });
      } else {
        request.post("/hotelBooking/addHotelBookings", this.form).then(res => {
          if (res.code === '0') {
            this.$message({
              type: "success",
              message: '新增成功'
            });
            this.dialogVisible = false;
            this.getBookingList();
          } else {
            this.$message({
              type: "error",
              message: "新增失败"
            });
          }
        });
      }
    },
    formatDate(row, column, cellValue) {
      if (cellValue) {
        return cellValue.split('T')[0];
      }
      return '';
    }
  },
  created() {
    this.getBookingList();
  }
}
</script>

<style scoped>
.el-input__inner {
  text-overflow: ellipsis;
  overflow: hidden;
  white-space: nowrap;
  border-color: #ffaa50;
  border-radius: 8px;
}

.el-button--primary {
  background-color: #ffaa50 !important;
  border-color: #ffaa50 !important;
}

.el-button--text {
  color: #ffaa50;
}

.el-table {
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.1);
  background-color: white;
}

.el-table th {
  background-color: #ffaa50;
  color: white;
  font-weight: bold;
}

.el-table tr:hover {
  background-color: #ffcc80;
}

.el-pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.el-dialog {
  border-radius: 12px;
  box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.1);
  background-color: white;
}

.el-dialog__header {
  background-color: #ffaa50;
  color: white;
  padding: 15px;
  font-weight: bold;
  border-radius: 12px 12px 0 0;
}

.el-dialog__body {
  padding: 20px;
}

.el-dialog__footer {
  padding: 10px 20px;
  text-align: right;
  border-top: 1px solid #ebebeb;
}

.el-form-item__label {
  color: #333;
  font-weight: bold;
}

.el-form-item__content {
  display: flex;
  align-items: center;
}

</style>